﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Veteran_cms.Models;
using Veteran_cms.HtmlHelpers;
using System.Text;
using THS.HtmlHelpers;
namespace Veteran_cms.Controllers
{
    [HandleError]
    public class ManageEmailTemplateController : BaseController
    {
        
        //
        // GET: /EmailTemplateManagerment/
        Veterandb ctx = new Veterandb();
        public int PageSize = 6;
        #region--Admin--
        public ActionResult IndexEmailTemplate(int page = 1)
        {
            //if (!IsAuthenticateAdmin())
            //    return RedirectToAction("LogOn", "DashBoard");
            var model = new HomeAdminListViewModel();
            IEnumerable<mail_template> EmailTemplates = ctx.mail_template.ToList<mail_template>();
            model.EmailTemplateList = EmailTemplates.Skip((page-1) * PageSize).Take(PageSize).ToList();
            model.PageInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = EmailTemplates.Count() };
            return View(model);

        }
        //pagign
        public ActionResult Paging(int page = 1)
        {
            //if (!IsAuthenticateAdmin())
            //    return RedirectToAction("LogOn", "DashBoard");
            var model = new HomeAdminListViewModel();
            IEnumerable<mail_template> EmailTemplates = ctx.mail_template.ToList<mail_template>();
            model.EmailTemplateList = EmailTemplates.Skip((page-1) * PageSize).Take(PageSize).ToList();
            model.PageInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = EmailTemplates.Count() };
            return PartialView("UCEmailTemplateList",model);

        }
        //
        // GET: /EmailTemplateManagerment/Details/5

        public ActionResult Details(int id)
        {
            try
            {
                 //if (!IsAuthenticateAdmin())
                 // return RedirectToAction("LogOn", "DashBoard");
                mail_template et = ctx.mail_template.Single(p => p.template_id == id);
                if (et != null)
                    return View(et);
                return View();
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /EmailTemplateManagerment/Create

        public ActionResult Create()
        {
            //if (!IsAuthenticateAdmin())
            //    return RedirectToAction("LogOn", "DashBoard");
            //EmailTemplate EmailTemplate = new EmailTemplate();
            return View();
        }

        //
        // POST: /EmailTemplateManagerment/Create

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create([Bind(Exclude = "template_id")] mail_template et)
        {
            // Attempt to register the EmailTemplate


            //if (!IsAuthenticateAdmin())
            //    return RedirectToAction("LogOn", "DashBoard");
            try
            {
                        ctx.AddTomail_template(et);
                        ctx.SaveChanges();
                        return RedirectToAction("IndexEmailTemplate");
                   
            }
            catch
            {
                return View("Create");
            }

        }

        //
        // POST: /EmailTemplateManagerment/Edit/5

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Details(int id ,FormCollection collection)
        {
            try
            {
                //if (!IsAuthenticateAdmin())
                //    return RedirectToAction("LogOn", "DashBoard");
                mail_template et = ctx.mail_template.Where(p => p.template_id == id).SingleOrDefault();
                

                    UpdateModel(et);
                    ctx.SaveChanges();
                    ViewData["Msg"] = "Cập nhật thành công";
                    return View("Details", et);
               
            }
            catch
            {
                ViewData["Msg"] = "Cập nhật không thành công";
                return View("Details");
            }
        }
        //allow Log On
        public void ActiveEmailTemplate(int id)
        {
            try
            {
                //if (!IsAuthenticateAdmin())
                //  return;
                mail_template et = ctx.mail_template.Where(p => p.template_id == id).SingleOrDefault();
               if (et.locked)
                   et.locked = false;
                else
                   et.locked = true;
                ctx.SaveChanges();
            }
            catch
            {
                return;
            }
        }
        
        //
        // GET: /EmailTemplateManagerment/Delete/5

        public ActionResult Delete(int id)
        {
            try
            {
                //if (!IsAuthenticateAdmin())
                //    return RedirectToAction("LogOn", "DashBoard");
                mail_template et = ctx.mail_template.Where(p => p.template_id == id).SingleOrDefault();
                ctx.mail_template.DeleteObject(et);
                ctx.SaveChanges();
                return RedirectToAction("IndexEmailTemplate");
            }
            catch
            {
                return View();
            }
        }
        


        #endregion


    }
}
